package hrms.controller;

import hrms.entity.Result;
import hrms.entity.Staff;
import hrms.entity.Statistic;
import hrms.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @GetMapping("/findStaffById")
    public Result findStaffById(Integer id){
        Staff staff = staffService.findById(id);

        System.out.println(staff);

        Map<String,Object> map = new HashMap<>();
        map.put("staff",staff);
        return Result.success().addMap(map);
    }

    @PostMapping("/find/{id}")
    public Result findDP(@PathVariable Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        Staff staff = staffService.findById(id);
        map.put("list",staff);
        return Result.success().addMap(map);
    }
    @GetMapping("/findStaffByName")
    public Result findStaffByName(String name){
        Staff staff = staffService.findStaffByName(name);
        Map<String,Object> map = new HashMap<>();
        map.put("staff",staff);
        return  Result.success().addMap(map);
    }

    @GetMapping("/findStaffByDepartmentId")
    public Result findStaffByDepartment_id(int id){
        List<Staff> staff = staffService.findStaffByDepartment_id(id);
        Map<String,Object> map = new HashMap<>();
        map.put("staffList",staff);
        return Result.success().addMap(map);
    }

    @GetMapping("/selectAllStaff")
    public Result selectAllStaff(){
        List<Staff> staff = staffService.selectAllStaff();
        Map<String, Object> map = new HashMap<>();
        map.put("staffList", staff);
        return Result.success().addMap(map);
    }

    @GetMapping("/statisticsGender")
    public Result statisticsGender(){
        List<Statistic>  statistics = staffService.statisticsGender();
        int sum=0;
        for (Statistic statistic : statistics) {
            sum+=statistic.getCount();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("genderList",statistics);
        map.put("genderSum", sum);
        return  Result.success().addMap(map);
    }

    @GetMapping("/statisticsPost")
    public Result statisticsPost(){
        List<Statistic> statistics = staffService.statisticsPost();
        Map<String,Object> map = new HashMap<>();
        map.put("postList",statistics);
        return Result.success().addMap(map);
    }

    @GetMapping("/statisticsEducation")
    public Result statisticsEducation(){
        List<Statistic> statistics = staffService.statisticsEducation();
        int sum=0;
        for (Statistic statistic : statistics) {
            sum+=statistic.getCount();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("educationList",statistics);
        map.put("educationSum", sum);
        return Result.success().addMap(map);
    }

}
